package org.example.emp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.emp.dto.EmployeeDto;
import org.example.emp.pojo.Department;
import org.example.emp.pojo.Employee;
import org.example.emp.pojo.Result;
import org.example.emp.service.DepartmentService;
import org.example.emp.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


//员工管理控制器
@Slf4j
@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private  EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    // 添加员工
    @PostMapping
    public Result addEmployee(@RequestBody Employee employee) {

        boolean b = employeeService.addEmployee(employee);
        if (b) {
            return new Result(0 , "添加成功");
        }
        return new Result(100 , "添加失败");
    }

    // 删除员工
    @DeleteMapping("/{id}")
    public Result deleteEmployee(@PathVariable Integer id) {
        boolean b = employeeService.deleteEmployee(id);
        if (b) {
            return new Result(0 , "删除成功");
        }
        return new Result(100 , "删除失败");
    }

    // 修改员工信息
    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee) {
        boolean b = employeeService.updateEmployee(employee);
        if (b) {
            return new Result(0 , "修改成功");
        }
        return new Result(100 , "修改失败");
    }

    // 查看员工信息以及对应的考勤信息
    @GetMapping("/{id}")
    public Result getEmployeeById(@PathVariable Integer id) {
        EmployeeDto employeeDto = employeeService.getEmployeeById(id);
        if (employeeDto == null) {
            return new Result(100 , "查询失败");
        }
        return new Result(0 , "查询成功" , employeeDto);
    }

    // 查看所有员工信息
    @GetMapping
    public Result getAllEmployees() {
        List<Employee> employees = employeeService.getAllEmployees();
       if(employees != null) {
           return new Result(0 , "查询成功" , employees);
       }
       return new Result(100 , "查询失败");
    }

    //员工分页查询
    @GetMapping("/page")
    public Result page(int pageNum , int pageSize){
        Page<Employee> pageInfo = new Page(pageNum, pageSize);
        Page<EmployeeDto> EmployeeDtoPage = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Employee::getEmployeeId);
        employeeService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, EmployeeDtoPage , "records");

        List<Employee> records = pageInfo.getRecords();
        List<EmployeeDto> employeeDtoList = records.stream().map((item) -> {
            EmployeeDto employeeDto = new EmployeeDto();
            BeanUtils.copyProperties(item, employeeDto);
            Integer departmentId = item.getDepartmentId();
            Department department = departmentService.getById(departmentId);
            if (department != null) {
                employeeDto.setDepartmentName(department.getDepartmentName());
            }
            return employeeDto;
        }).collect(Collectors.toList());
        EmployeeDtoPage.setRecords(employeeDtoList);
        return new Result<>(0 , "查询成功" , EmployeeDtoPage);
    }

    //单个员工/批量员工 分配部门
    @PutMapping("{id}")
    public Result updateEmployeeAndDp(@PathVariable Integer id , Integer[] ids) {
        log.info("id = " + id);
        log.info("ids.length = " + ids.length);
        List<Integer> list = new ArrayList<>();
        for (int i : ids) {
            list.add(i);
        }
        boolean temp = employeeService.updateEmployeeDepartment(list, id);
        if (temp) {
            return new Result(0 , "修改部门成功");
        }
        return new Result(100 , "修改部门失败");
    }
}